package com.wcl.demo.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wcl.demo.R;
import com.wcl.demo.db.BackGroundHelper;
import com.wcl.demo.db.NoteBgHelper;
import com.wcl.demo.model.entity.BackGroundData;
import com.wcl.demo.model.entity.NoteBgData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/11/28.
 */
public class ChosePhotoActivity extends Activity implements View.OnClickListener {
    private ImageView activity_chose_photoIv;
    private static int RESULT_LOAD_IMAGE = 1;
    private TextView chose_photo_retry_Btn, chose_use_picture_Btn;
    private String picturePath;
    private String chosePicture_key;//用于区别哪个activity调用的ChosePhotoActivity;
    private BackGroundHelper backGroundHelper;
    private NoteBgHelper noteBgHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chose_photo);
        initView();
        initData();

    }


    private void initData() {
        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        startActivityForResult(i, RESULT_LOAD_IMAGE);

        backGroundHelper = new BackGroundHelper();
        noteBgHelper=new NoteBgHelper();
    }

    private void initView() {
        activity_chose_photoIv = (ImageView) findViewById(R.id.activity_chose_photoIv);
        chose_use_picture_Btn = (TextView) findViewById(R.id.chose_use_picture_Btn);
        chose_use_picture_Btn.setOnClickListener(this);

        chose_photo_retry_Btn = (TextView) findViewById(R.id.chose_photo_retry_Btn);
        chose_photo_retry_Btn.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            Log.i("shaco", data.getData() + "");
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            Log.i("shaco", picturePath + "<<<<<<----------");
            cursor.close();

//            ImageView imageView = (ImageView) findViewById(R.id.activity_chose_photoIv);
            activity_chose_photoIv.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.chose_photo_retry_Btn:
                finish();
                break;
            case R.id.chose_use_picture_Btn:
                chosePicture_key = getIntent().getStringExtra("chosePicture_key");//得到chosePicture_key
                if (chosePicture_key.equals("1")) {

                    Intent intent = new Intent();
                    intent.putExtra("wayKey", "1");
                    intent.putExtra("picturePath", picturePath);
                    intent.setClass(this, AroundActivity.class);
                    startActivity(intent);
                    finish();
                } else if (chosePicture_key.equals("2")) {

                    if (backGroundHelper != null) {
                        backGroundHelper.deleteBg();
                    }
                    List<BackGroundData> list = new ArrayList<>();
                    BackGroundData backGroundData = new BackGroundData();
                    backGroundData.setPictureKey(picturePath);
                    list.add(backGroundData);
                    backGroundHelper.saveBgInfor(list);

                    Toast.makeText(this, "设置完成", Toast.LENGTH_SHORT).show();
                    finish();
                }else if(chosePicture_key.equals("3")){//保存note背景墙的点击事件
                    if (noteBgHelper != null) {
                        noteBgHelper.deleteBg();
                    }
                    List<NoteBgData> list_note = new ArrayList<>();
                    NoteBgData noteBgData = new NoteBgData();
                    noteBgData.setPictureKey(picturePath);
                    list_note.add(noteBgData);
                    noteBgHelper.saveBgInfor(list_note);

                    Toast.makeText(this, "设置完成", Toast.LENGTH_SHORT).show();
                    finish();

                }

                break;
        }
    }
}
